(function(angular) {

var app = angular.module('myApp', []);  
app.controller('myCart', function($scope) {  

         $scope.cartList=[{  
        id:0,  
        image:"images/1.jpg",  
        name:"Casio/卡西欧 EX-TR350",  
        price:5999.88,  
        num:1,  
        total:5999.88  
    },  
    {     
        id:1,  
        image:"images/2.jpg",  
        name:"Canon/佳能 PowerShot SX50 HS",  
        price:3888.50,  
        num:1,  
        total:3888.50  
    },  
    {     
        id:2,  
        image:"images/3.jpg",  
        name:"Sony/索尼 DSC-WX300",  
        price:1428.50,  
        num:1,  
        total:1428.50  
    },  
    {     
        id:3,  
        image:"images/4.jpg",  
        name:"Fujifilm/富士 instax mini 25",  
        price:640.60,  
        num:1,  
        total:640.60  
    }]  
	 $scope.checkAll=function(){  
            for(var i in $scope.cartList){  
                 $scope.cartList[i].check=$scope.all;  
            }  
        }  
        $scope.deleteCart=function(id){  
        for(var i in $scope.cartList){  
                if($scope.cartList[i].id==id){ 
                	  var con = confirm('确定删除所选商品吗？');
                	  if(con){
                	  	$scope.cartList.splice(i,1);  //splice() 方法向/从数组中添加/删除项目，然后返回被删除的项目。
                	  }
            }  
        }  
    }  
    $scope.deleteAll=function(){
    	var con=confirm('确定删除所选的商品?');
    	if(con){
    		for(var i in $scope.cartList){
    		 	if($scope.cartList[i].check){
    				 var id= $scope.cartList[i].id;
    				
    				}
    		 	
    			
    		}
    	}
    	
    }
	$scope.add = function(index) {
		$scope.cartList[index].num++; 
	};
	
	$scope.reduce=function(index){
		if(	$scope.cartList[index].num<=1){
			alert("请检查数量");
		}else{
			$scope.cartList[index].num--; 
			 
		}

	}
    
    $scope.$watch('cartList',function(newValue,oldValue,scope){  
        $scope.count=0; //计数器  
        $scope.sumTotal=0; //总计 
        $scope.ss=0;
        for(var i in newValue){  
            var total=newValue[i].num*newValue[i].price; //计算出新的结果  
            $scope.cartList[i].total=total.toFixed(3); //保留三位小数并且把它赋值给元数据;    
            if(newValue[i].check){ 
            	$scope.ss+=newValue[i].num;
                $scope.sumTotal+=total;  
                $scope.count++; 
            }  
              
        }  
            $scope.all=($scope.count==newValue.length); //如果所有的都选中的话，那么全选也应该选中  
},true); 

	










});
})(angular);